#!/usr/bin/env bash

__rvm_detect_system_from_os_release()
{
  local __system_name="$( awk -F'=' '$1=="ID"{print $2}' /etc/os-release | head -n 1 | tr '[A-Z]' '[a-z]' | tr -d \" )"

  case $__system_name in
    amzn*)
      _system_name="Amazon"
      _system_version="$( awk -F'=' '$1=="VERSION_ID"{gsub(/"/,"");print $2}' /etc/os-release | head -n 1 )"
      _system_arch="$( uname -m )"
      ;;

    sles*)
      _system_name="SuSE"
      _system_version="$( awk -F'=' '$1=="VERSION_ID"{gsub(/"/,"");print $2}' /etc/os-release | head -n 1 )"
      _system_arch="$( uname -m )"
      ;;

    opensuse*)
      _system_name="OpenSuSE"
      _system_version="$( awk -F'=' '$1=="VERSION_ID"{gsub(/"/,"");print $2}' /etc/os-release | head -n 1 )"
      _system_arch="$( uname -m )"
      ;;

    pclinuxos*)
      _system_name="PCLinuxOS"
      _system_version="$(GREP_OPTIONS="" \command \grep -Eo '[0-9\.]+' /etc/redhat-release  | \command \awk -F. '{print $1}' | head -n 1)"
      _system_arch="$( uname -m )"
      ;;

    void*)
      _system_name="Void"
      _system_version="$(\command \lsb_release -a | \command awk -F: '/Release/{gsub(" |\t",""); print $2}')"
      _system_arch="$( uname -m )"
      ;;

    debian*)
      _system_name="Debian"
      _system_version="$(awk -F'=' '$1=="VERSION_ID"{gsub(/"/,"");print $2}' /etc/os-release | \command \awk -F. '{print $1}' | head -n 1)"
      _system_arch="$( dpkg --print-architecture )"

      if
        [ -z "$_system_version" ]
      then
        _system_version="$(\command \cat /etc/debian_version | \command \awk -F. '{print $1}' | head -n 1)"
      fi

      __rvm_detect_debian_major_version_from_codename
      ;;

    *)
      return 1
  esac

  return 0
}
